package com.alex.server.controller;

import com.alex.server.pojo.Admin;
import com.alex.server.pojo.RespBean;
import com.alex.server.service.IAdminService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 个人中心
 */
@RestController
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    public PasswordEncoder passwordEncoder;
    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    //authentication是认证,里面带有该用户的所有权限
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        if (adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(
                    new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success("更新成功");
        }else {
            return RespBean.error("更新失败");
        }
    }
    @ApiOperation(value = "更新用户密码")
    @PutMapping("/admin/pass")
    public RespBean updatePassword(@RequestBody Map<String ,Object> info){
        //这是老密码
        String oldPass = (String)info.get("oldPass");
        //这是新密码
        String newPass = (String) info.get("pass");
        //这是用户的Id
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updateAdminPassword(oldPass,newPass,adminId);
    }
}
